package com.letv.business.flow.LeMPSession;

import com.ledim.app.LeDimApplication;
import com.letv.android.client.activity.AlbumPlayActivity;
import com.letv.business.flow.PlayFlowEvents.FetchMetaEvent;
import com.letv.business.flow.PlayFlowEvents.PlayIdleEvent;
import com.letv.business.flow.PlayFlowModule.NetworkManager;
import com.letv.business.flow.PlayFlowState.PlayErrorState;
import com.letv.business.flow.PlayFlowState.PlayFlowActionInterface;

import android.app.FragmentManager;
import android.content.Context;
import android.view.View;
import android.widget.Toast;

import com.letv.android.young.client.R;
import com.letv.business.flow.PlayFlowState.PlayAdState;
import com.letv.business.flow.PlayFlowState.PlayAuthState;
import com.letv.business.flow.PlayFlowState.PlayCacheLoadState;
import com.letv.business.flow.PlayFlowState.PlayFetchMetaState;
import com.letv.business.flow.PlayFlowState.PlayFlowActionInterface;
import com.letv.business.flow.PlayFlowState.PlayFlowEvents;
import com.letv.business.flow.PlayFlowState.PlayFlowProvider;
import com.letv.business.flow.PlayFlowState.PlayIdleState;
import com.letv.business.flow.PlayFlowState.PlayOnCallState;
import com.letv.business.flow.PlayFlowState.PlayReCordCheckState;
import com.letv.business.flow.PlayFlowState.PlayStartPlayState;
import com.letv.business.flow.PlayFlowState.PlayUrlPrepareState;
import com.letv.business.flow.album.AlbumPlayFlow;
import com.letv.core.utils.LogInfo;
import com.polidea.statemachine.State;
import com.polidea.statemachine.StateMachine;
import com.polidea.statemachine.StateProvider;
import com.polidea.statemachine.handler.BaseStateableHandler;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by jipengfei on 2016/10/31.
 */
public  class PlayStateableHandler extends BaseStateableHandler<PlayFlowProvider,PlayFlowActionInterface> implements PlayFlowProvider, PlayFlowActionInterface {

    private  AlbumPlayFlow mPlayFlow;
    private  AlbumPlayActivity mPlayActivity;


    public  PlayStateableHandler(AlbumPlayActivity albumPlayActivity){
        super(albumPlayActivity);
       LeDimApplication.getComponentInstance().inject(this);

        mPlayActivity = albumPlayActivity;
    }


    @Override
    public PlayFlowProvider getStateProvider() {
        return this;
    }

    @Override
    public PlayFlowActionInterface getActionInterface() {
        return this;
    }


//    @Override
//    public FragmentManager provideFragmentManager() {
//        return mainActivity.getSupportFragmentManager();
//    }

//    @Override
//    public int provideFragmentContainerId() {
//        return R.id.fragment_container;
//    }
    @Override
    public AlbumPlayFlow providPlayFlow(){
        mPlayFlow = mPlayActivity.getFlow();
        return  mPlayFlow;
    }

    @Override
   public void fetchMeta(){

    }

    @Override
    public void onStateMachineDescribe(StateMachine<PlayFlowProvider, PlayFlowActionInterface> stateMachine) {
        // idle状态下状态迁移
//        stateMachine.addTransitionFromClass(PlayIdleState.class, PlayFlowEvents.Play_RecordCheck, PlayReCordCheckState.class);
        stateMachine.addTransitionFromClass(PlayIdleState.class, PlayFlowEvents.Play_FetchMeta, PlayFetchMetaState.class);

//        // 历史播放查询状态下状态迁移
//        stateMachine.addTransitionFromClass(PlayReCordCheckState.class, PlayFlowEvents.Play_FetchMeta, PlayFetchMetaState.class);
//        stateMachine.addTransitionFromClass(PlayReCordCheckState.class, PlayFlowEvents.Play_CacheLoad, PlayCacheLoadState.class);
//
        // 播放地址获取状态迁移
        stateMachine.addTransitionFromClass(PlayFetchMetaState.class, PlayFlowEvents.Play_Auth, PlayAuthState.class);
        stateMachine.addTransitionFromClass(PlayFetchMetaState.class, PlayFlowEvents.Play_UrlPrepare, PlayUrlPrepareState.class);
        stateMachine.addTransitionFromClass(PlayFetchMetaState.class, PlayFlowEvents.Play_Idle, PlayIdleState.class);

//        // 本地缓存查询状态迁移
//        stateMachine.addTransitionFromClass(PlayCacheLoadState.class, PlayFlowEvents.Play_Auth, PlayAuthState.class);
//        stateMachine.addTransitionFromClass(PlayCacheLoadState.class, PlayFlowEvents.Play_Idle, PlayIdleState.class);
//        stateMachine.addTransitionFromClass(PlayCacheLoadState.class, PlayFlowEvents.Play_FetchMeta, PlayCacheLoadState.class);
//
        // 播放权限鉴权状态迁移
        stateMachine.addTransitionFromClass(PlayAuthState.class, PlayFlowEvents.Play_UrlPrepare, PlayUrlPrepareState.class);
//
//
        // 播放地址解析状态迁移
        stateMachine.addTransitionFromClass(PlayUrlPrepareState.class, PlayFlowEvents.Play_StartPlay, PlayStartPlayState.class);
        stateMachine.addTransitionFromClass(PlayUrlPrepareState.class, PlayFlowEvents.Play_AdData, PlayAdState.class);
        stateMachine.addTransitionFromClass(PlayUrlPrepareState.class, PlayFlowEvents.Play_Idle, PlayIdleState.class);

//
//        // 获取广告状态迁移
//        stateMachine.addTransitionFromClass(PlayAdState.class, PlayFlowEvents.Play_StartPlay, PlayStartPlayState.class);
//
        // 播放过程状态迁移
        stateMachine.addTransitionFromClass(PlayStartPlayState.class, PlayFlowEvents.Play_Idle, PlayIdleState.class);
//        stateMachine.addTransitionFromClass(PlayStartPlayState.class, PlayFlowEvents.Play_UrlPrepare, PlayUrlPrepareState.class);
//        stateMachine.addTransitionFromClass(PlayStartPlayState.class, PlayFlowEvents.Play_OnCall, PlayOnCallState.class);
//
//        // 待命状态迁移
//        stateMachine.addTransitionFromClass(PlayOnCallState.class, PlayFlowEvents.Play_Idle, PlayIdleState.class);

        //播放失败
        stateMachine.addTransitionFromClass(PlayAuthState.class, PlayFlowEvents.Play_Error, PlayErrorState.class);
        stateMachine.addTransitionFromClass(PlayFetchMetaState.class, PlayFlowEvents.Play_Error, PlayErrorState.class);

       //播放流程结束,重置为idle状态
        stateMachine.addTransitionFromClass(PlayStartPlayState.class, PlayFlowEvents.Play_Finish, PlayIdleState.class);
        stateMachine.addTransitionFromClass(PlayErrorState.class, PlayFlowEvents.Play_Finish, PlayIdleState.class);


    }

    @Override
    public Class<? extends State> getInitialStateClass() {
        return PlayIdleState.class;
    }




}
